package io.zhifei.modules.sys.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.R;
import io.zhifei.modules.sys.system.form.SysDictParam;
import io.zhifei.modules.sys.system.form.SysDictResult;
import io.zhifei.modules.sys.system.service.SysDictService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>功能描述: 字典控制层</p>
 *
 * @author : duke
 * @version 1.0.0<br>
 * @CreateTime 2020/11/11
 */
@RequestMapping("/sys/dict")
@RestController
@Api(value = "字典Controller",tags = "后台字典管理")
public class SysDictController extends AbstractController{

    @Resource
    private SysDictService dictService;


    /**
     * 新增字典
     * @param param
     * @return
     */
    @PutMapping("/addDict")
    @ApiOperation(value = "添加字典",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pCode",value = "字典父级code",required = true),
    })
    public R addDict(SysDictParam param){
        param.setCreatTime(new Date());
        param.setCreatUser(getUserId());
        this.dictService.addDict(param);
        return R.ok();
    }

    /**
     * 更新字典
     * @param param
     * @return
     */
    @PostMapping("/editDict")
    @ApiOperation(value = "更新字典",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "字典ID",required = true),
    })
    public R editDict(SysDictParam param){
        param.setUpdateTime(new Date());
        param.setUpdateUser(getUserId());
        this.dictService.updateDict(param);
        return R.ok();
    }

    /**
     * 删除字典
     * @param param
     * @return
     */
    @DeleteMapping("deleteDict")
    @ApiOperation(value = "删除字典",httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "字典ID",required = true),
    })
    public R deleteDict(@ApiIgnore SysDictParam param){
        param.setUpdateTime(new Date());
        param.setUpdateUser(getUserId());
        param.setIsDel(1);
        this.dictService.updateDict(param);
        return R.ok();
    }

    /**
     * 获取字典分页列表
     * @param page 分页参数
     * @param param 查询参数
     * @return
     */
    @GetMapping("/getPageList")
    @ApiOperation(value = "获取字典分页列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam( name = "current",value = "当前页", required = true),
            @ApiImplicitParam( name = "size",value = "页面大小", required = true),
            @ApiImplicitParam(name = "name",value = "字典名称"),
            @ApiImplicitParam(name = "pCode",value = "字典父级编码code"),
            @ApiImplicitParam(name = "status",value = "状态，默认为1，1：展示、0：不展示，2：联动展示",required = true),
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功",response = SysDictResult.class)
    })
    public R getPageList(@ApiIgnore Page page,@ApiIgnore SysDictParam param){
        PageUtils dictPageList = this.dictService.getPageList(page,param);
        return R.ok().put("data",dictPageList);
    }

    /**
     * 获取字典列表
     * @param param 查询参数
     * @return
     */
    @GetMapping("/getList")
    @ApiOperation(value = "获取字典列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pCode",value = "字典父级编码code",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功",response = SysDictResult.class)
    })
    public R getList(@ApiIgnore SysDictParam param){
        List<SysDictResult> dictList = this.dictService.getList(param);
        return R.ok().put("data",dictList);
    }

    /**
     * 获取字典详情
     * @param param 字典ID
     * @return
     */
    @GetMapping("/getDetail")
    @ApiOperation(value = "获取字典详情",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "字典ID",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功",response = SysDictResult.class)
    })
    public R getDetail(@ApiIgnore SysDictParam param){
        SysDictResult dictResult = this.dictService.getDetail(param);
        return R.ok().put("data",dictResult);
    }

    /**
     * 获取字典树状列表
     * @param param 查询参数
     * @return
     */
    @GetMapping("/getTreeList")
    @ApiOperation(value = "获取字典树状列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid",value = "字典父级编码ID",required = true)
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "请求成功",response = SysDictResult.class)
    })
    public R getTreeList(@ApiIgnore SysDictParam param){
        List<SysDictResult> dictList = this.dictService.getTreeList(param);
        return R.ok().put("data",dictList);
    }


}
